package cases

import (
	"github.com/beego/beego/v2/client/orm"
	"github.com/beego/beego/v2/server/web"
	"github.com/jinzhu/copier"
	"strconv"
	"time"
	"xpms/app/models"
	"xpms/cores"
	"xpms/cores/utils"
)

type TestCasePkg struct {
	Id         int64 `orm:"pk;column(id);" json:"Id,string"`
	Projectid  int64 `json:"Projectid,string"`
	Name       string
	Acceptid   int64  `json:"Acceptid,string"`
	Acceptname string `json:"Acceptname,string"`
	Remarks    string
	Attachment string
	Created    int64
	Createid   int64 `json:"Createid,string"`
	Status     int
	Changed    int64
	Changid    int64 `json:"Changid,string"`
	Sort       int64 `json:"Sort,string"`
}

func (this *TestCasePkg) TableName() string {
	return models.TableName("projects_test_case_pkg")
}

func init() {
	orm.RegisterModel(new(TestCasePkg))
}

func AddCasePkg(upd TestCasePkg) error {
	o := orm.NewOrm()
	pro := new(TestCasePkg)
	upd.Created = time.Now().Unix()
	upd.Changed = time.Now().Unix()
	copier.Copy(pro, upd)
	_, err := o.Insert(pro)
	return err
}

func UpdateCasePkg(upd TestCasePkg) error {
	o := orm.NewOrm()
	var col []string
	col = append(col, "changed")
	if !utils.IsEmpty(upd.Name) {
		col = append(col, "name")
	}
	if !utils.IsEmpty(upd.Acceptid) {
		col = append(col, "acceptid")
	}
	if !utils.IsEmpty(upd.Acceptname) {
		col = append(col, "acceptname")
	}
	upd.Changed = time.Now().Unix()
	_, err := o.Update(&upd, utils.RemoveDuplicatesAndEmpty(col)...)
	return err
}

func GetCasePkgByname(name string) (TestCasePkg, error) {
	var module TestCasePkg
	var err error
	o := orm.NewOrm()
	o.QueryTable(models.TableName("projects_test_case_pkg")).Filter("name", name).One(&module)
	if err == orm.ErrNoRows {
		return module, nil
	}
	return module, err
}
func GetCasePkgById(id int64) (TestCasePkg, error) {
	var Dto TestCasePkg
	var err error
	o := orm.NewOrm()
	sql := "SELECT * FROM pms_projects_test_case_pkg WHERE id =?"
	_ = o.Raw(sql, id).QueryRow(&Dto)
	return Dto, err
}
func CasePkgNoPageExec(condArr map[string]interface{}) []TestCasePkg {
	o := orm.NewOrm()
	var dto []TestCasePkg
	o.Raw(casePkgPageSql(condArr, 1, 1, 100000)).QueryRows(&dto)
	return dto
}

func CasePkgPageExec(condArr map[string]interface{}, page int, offset int) (ops []TestCasePkg) {
	o := orm.NewOrm()
	var dto []TestCasePkg
	o.Raw(casePkgPageSql(condArr, 1, page, offset)).QueryRows(&dto)
	return dto
}

func CasePkgPageCountExec(condArr map[string]interface{}, page int, offset int) int64 {
	o := orm.NewOrm()
	var ProjectsModuleDto int64
	o.Raw(casePkgPageSql(condArr, 0, page, offset)).QueryRow(&ProjectsModuleDto)
	return ProjectsModuleDto
}

/**
  组装sql
*/
func casePkgPageSql(condArr map[string]interface{}, ifsCount, page int, offset int) (string, []interface{}) {
	var sql string
	if ifsCount == 0 {
		sql = sql + `SELECT count(1) FROM (`
	}
	sql = sql + `
	SELECT
	  pkg.*
	FROM pms_projects_test_case_pkg pkg
	WHERE 1=1 `
	param := make([]interface{}, 0, 0)
	if !utils.IsEmpty(condArr["id"]) {
		sql = sql + ` and  pkg.id=?`
		param = append(param, condArr["id"])
	}
	if !utils.IsEmpty(condArr["projectid"]) {
		sql = sql + ` and  pkg.projectid=?`
		param = append(param, condArr["projectid"])
	}
	if !utils.IsEmpty(condArr["keyword"]) {
		sql = sql + ` and  pkg.name LIKE concat('%',?,'%')`
		param = append(param, condArr["keyword"])
	}
	if !utils.IsEmpty(condArr["acceptid"]) {
		sql = sql + ` and  pkg.acceptid in(`
		sql1, value := cores.SqlInParam(condArr["acceptid"])
		sql = sql + sql1 + `)`
		param = append(param, value...)
	}
	if !utils.IsEmpty(condArr["ids"]) {
		sql = sql + ` and  pkg.id in(`
		sql1, value := cores.SqlInParam(condArr["ids"])
		sql = sql + sql1 + `)`
		param = append(param, value...)
	}
	if ifsCount == 0 {
		sql = sql + ` ) C`
	} else if ifsCount > 0 {
		if page < 1 {
			page = 1
		}
		if offset < 1 {
			offset, _ = web.AppConfig.Int("pageoffset")
		}
		start := (page - 1) * offset
		startStr := strconv.Itoa(start)
		offsetStr := strconv.Itoa(offset)
		sql = sql + ` order by pkg.id  LIMIT ` + startStr + `,` + offsetStr
	}
	return sql, param
}
